#!/bin/bash

# Agent架构测试脚本

echo "🤖 开始测试Agent架构..."

# 服务器地址
SERVER_URL="http://localhost:8080"
SESSION_ID="test-agent-$(date +%s)"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 测试函数
test_api() {
    local name="$1"
    local method="$2"
    local url="$3"
    local data="$4"
    
    echo -e "${BLUE}测试: $name${NC}"
    
    if [ "$method" = "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" "$url")
    else
        response=$(curl -s -w "\n%{http_code}" -X "$method" -H "Content-Type: application/json" -d "$data" "$url")
    fi
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✅ 成功 (HTTP $http_code)${NC}"
        echo "响应: $body" | head -c 200
        echo "..."
        echo ""
    else
        echo -e "${RED}❌ 失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
        echo ""
        return 1
    fi
}

# 等待服务器启动
echo "⏳ 等待服务器启动..."
for i in {1..30}; do
    if curl -s "$SERVER_URL" > /dev/null 2>&1; then
        echo -e "${GREEN}✅ 服务器已启动${NC}"
        break
    fi
    if [ $i -eq 30 ]; then
        echo -e "${RED}❌ 服务器启动超时${NC}"
        exit 1
    fi
    sleep 1
done

echo ""
echo "🔧 测试Agent工具列表..."
test_api "获取可用工具" "GET" "$SERVER_URL/api/agent/tools"

echo ""
echo "📊 测试Agent统计信息..."
test_api "获取Agent统计" "GET" "$SERVER_URL/api/agent/stats"

echo ""
echo "🕐 测试时间查询功能..."
test_api "时间查询" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "现在北京时间是几点？"
}'

echo ""
echo "🧮 测试数学计算功能..."
test_api "数学计算" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "计算 15 + 27"
}'

echo ""
echo "💬 测试普通聊天功能..."
test_api "普通聊天" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "你好，请介绍一下你的能力"
}'

echo ""
echo "🧠 测试记忆搜索功能..."
test_api "记忆搜索" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "我们刚才聊过什么？"
}'

echo ""
echo "📚 测试获取历史记录..."
test_api "获取历史" "GET" "$SERVER_URL/api/chat/$SESSION_ID/history"

echo ""
echo "🌊 测试流式响应..."
echo -e "${BLUE}测试: 流式响应${NC}"
curl -s -X POST -H "Content-Type: application/json" \
    -d '{"sessionId": "'$SESSION_ID'", "message": "请用流式方式回答：什么是人工智能？"}' \
    "$SERVER_URL/api/chat/stream" | head -n 10
echo -e "${GREEN}✅ 流式响应测试完成${NC}"

echo ""
echo "🧮 测试复杂计算..."
test_api "复杂计算" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "计算 sqrt(16) + pow(2,3)"
}'

echo ""
echo "🌍 测试多地点时间查询..."
test_api "多地点时间" "POST" "$SERVER_URL/api/chat" '{
    "sessionId": "'$SESSION_ID'",
    "message": "纽约现在几点了？"
}'

echo ""
echo "📊 测试最终Agent统计..."
test_api "最终统计" "GET" "$SERVER_URL/api/agent/stats"

echo ""
echo "🗑️ 测试清除历史..."
test_api "清除历史" "DELETE" "$SERVER_URL/api/chat/$SESSION_ID"

echo ""
echo -e "${GREEN}🎉 Agent架构测试完成！${NC}"

# 性能测试
echo ""
echo "⚡ 开始性能测试..."
echo "发送10个并发请求..."

for i in {1..10}; do
    (
        session_id="perf-test-$i"
        curl -s -X POST -H "Content-Type: application/json" \
            -d '{"sessionId": "'$session_id'", "message": "测试消息 '$i'"}' \
            "$SERVER_URL/api/chat" > /dev/null
        echo "请求 $i 完成"
    ) &
done

wait
echo -e "${GREEN}✅ 性能测试完成${NC}"

echo ""
echo "📋 测试总结:"
echo "- ✅ Agent工具管理"
echo "- ✅ 智能意图识别"
echo "- ✅ 计划制定和执行"
echo "- ✅ 记忆管理"
echo "- ✅ 时间查询工具"
echo "- ✅ 数学计算工具"
echo "- ✅ 记忆搜索工具"
echo "- ✅ 流式响应"
echo "- ✅ 并发处理"
echo ""
echo -e "${GREEN}🚀 Agent架构优化完成并测试通过！${NC}"
